package info.javatech.jmf.client.util;

import info.javatech.jmf.client.controller.exceptions.RegistryElementNotFoundException;
import info.javatech.jmf.client.controller.wsclient.PComponent;
import info.javatech.jmf.common.components.CommandConstants;
import info.javatech.jmf.common.components.FormConstants;
import info.javatech.jmf.common.components.TextFieldConstants;

public class ComponentRegistry {
    
    private static String COMPONENT_FORM_WRAPPER_CLASSNAME = "info.javatech.jmf.client.wrappers.FormWrapper";
    private static String COMPONENT_FORM_CLASSNAME = "javax.microedition.lcdui.Form";
    
    private static String COMPONENT_COMMAND_WRAPPER_CLASSNAME = "info.javatech.jmf.client.wrappers.CommandWrapper";
    private static String COMPONENT_COMMAND_CLASSNAME = "javax.microedition.lcdui.Command";
    
    private static String COMPONENT_TEXTFIELD_WRAPPER_CLASSNAME = "info.javatech.jmf.client.wrappers.TextFieldWrapper";
    private static String COMPONENT_TEXTFIELD_CLASSNAME = "javax.microedition.lcdui.TextField";
    
    private final static int COMPONENTS_COUNT = 3;
    /**
     * wrapperClassName,j2meComponentClassName,pcomponentType
     */
    static String[][] componentRegistryTable = new String[3][COMPONENTS_COUNT];
    
    //TODO temporary initialization section - need to replace by normal, i.e. ititialize it from file or something else
    static
    {
        componentRegistryTable[0] = new String[]{COMPONENT_FORM_WRAPPER_CLASSNAME,COMPONENT_TEXTFIELD_WRAPPER_CLASSNAME,COMPONENT_COMMAND_WRAPPER_CLASSNAME};
        componentRegistryTable[1] = new String[]{COMPONENT_FORM_CLASSNAME,COMPONENT_TEXTFIELD_CLASSNAME,COMPONENT_COMMAND_CLASSNAME};
        componentRegistryTable[2] = new String[]{FormConstants.TYPE_NAME,TextFieldConstants.TYPE_NAME,CommandConstants.TYPE_NAME};
    }
    /** Creates a new instance of ComponentRegistry */
    public ComponentRegistry() {
    }
    
    public static final String getWrapperClassNameByComponent(PComponent pc) throws RegistryElementNotFoundException {
        for(int i = 0;i<componentRegistryTable[2].length;i++)
            if (componentRegistryTable[2][i].equals(pc.getType())) {
            return componentRegistryTable[0][i];
            }
        throw new RegistryElementNotFoundException();
    }
    
    public static final String getWrapperClassByJ2meComponent(Object object) throws RegistryElementNotFoundException {
        for(int i = 0;i<componentRegistryTable[1].length;i++)
            if (componentRegistryTable[1][i].equals(object.getClass().getName())) {
            return componentRegistryTable[0][i];
            }
        throw new RegistryElementNotFoundException();
    }
    
    public static final String[] getWrapperTypes() {
        String[] wrapperList = new String[componentRegistryTable[0].length];
        System.arraycopy(componentRegistryTable[0],0,wrapperList,0,componentRegistryTable[0].length);
        return wrapperList;
    }
}
